package com.chudian.task;

import com.chudian.entity.OrderInfo;
import com.chudian.enums.PayType;
import com.chudian.service.AlipayService;
import com.chudian.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : ZhangCheng
 * @date Date : 2022年11月11日
 */
@Component
@Slf4j
public class AlipayTask {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private AlipayService alipayService;

    /**
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm() throws Exception {
        log.info("支付宝查询超时订单");

        List<OrderInfo> orderInfoList = orderInfoService.queryNoPayOrderDuration(5, PayType.ALIPAY.getType());

        for (OrderInfo orderInfo: orderInfoList) {
            String orderNo = orderInfo.getOrderNo();
            log.warn("支付宝超时订单的订单号===>{}",orderNo);

            //核实订单状态：调用支付宝查单接口
            alipayService.checkOrderStatus(orderNo);

        }
    }

}
